//
//  UIView+borderView.m
//  ibgs_patients
//
//  Created by xiao.zhao@lachesis-mh.com on 15/12/29.
//  Copyright © 2015年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "UIView+borderView.h"

#import <objc/runtime.h>

static const void *kBorderWidthKey = &kBorderWidthKey;
static const void *kBorderColorKey = &kBorderColorKey;
static const void *kBorderDirectionKey = &kBorderDirectionKey;

@implementation UIView (borderView)

-(void)borderWithWidth:(CGFloat)width direction:(LXBorderDirection)direction
{
    [self borderWithWidth:width color:[UIColor lightGrayColor] direction:direction];
}

-(void)borderWithWidth:(CGFloat)width color:(UIColor *)color direction:(LXBorderDirection)direction
{
    self.borderWidth = width;
    self.borderColor = color;
    self.borderDirection = direction;
    
    [self drawBorder];
}

-(void)drawBorder
{
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, self.borderWidth);
    CGContextSetLineJoin(context, kCGLineJoinBevel);
    CGContextSetStrokeColorWithColor(context, self.borderColor.CGColor);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    if((self.borderDirection & LXBorderTop))
    {
        [path moveToPoint:CGPointZero];
        [path addLineToPoint:CGPointMake(self.bounds.size.width, 0)];
    }
    if ((self.borderDirection & LXBorderBottom))
    {
        [path moveToPoint:CGPointMake(0, self.bounds.size.height)];
        [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)];
    }
    if ((self.borderDirection & LXBorderLeft))
    {
        [path moveToPoint:CGPointZero];
        [path addLineToPoint:CGPointMake(0, self.bounds.size.height)];
    }
    if ((self.borderDirection & LXBorderRight))
    {
        [path moveToPoint:CGPointMake(self.bounds.size.width, 0)];
        [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)];
    }
    
    CGContextAddPath(context, path.CGPath);
    CGContextStrokePath(context);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.bounds];
    imageView.image = image;
    [self addSubview:imageView];
    [self sendSubviewToBack:imageView];
}

#pragma mark -getter and setter
-(CGFloat)borderWidth
{
    return [objc_getAssociatedObject(self, kBorderWidthKey) floatValue];
}

-(void)setBorderWidth:(CGFloat)borderWidth
{
    objc_setAssociatedObject(self, kBorderWidthKey, @(borderWidth), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIColor *)borderColor
{
    return objc_getAssociatedObject(self, kBorderColorKey);
}

-(void)setBorderColor:(UIColor *)borderColor
{
    objc_setAssociatedObject(self, kBorderColorKey, borderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(LXBorderDirection)borderDirection
{
    return [objc_getAssociatedObject(self, kBorderDirectionKey) unsignedIntegerValue];
}

-(void)setBorderDirection:(LXBorderDirection)borderDirection
{
    objc_setAssociatedObject(self, kBorderDirectionKey, @(borderDirection), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
